﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetDon.OA.Interface.Common;

using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;


namespace NetDon.OA.Business.Common
{
    public class AttachmentService:IAttachmentService
    {
        public bool AddAttachmentInfo(List<AttachmentData> attachments)
        {
            if(attachments==null)
            {
                return false;
            }
            foreach (var attachmentData in attachments)
            {
                AddAttachmentInfo(attachmentData);
            }
            return true;
        }

        public bool AddAttachmentInfo(AttachmentData attachment)
        {
            var entity = ConvertToEntityByBusinessData(attachment, true);
            return entity.Save();
        }

        public bool ModifyAttachmentInfo(AttachmentData attachment)
        {
            var entity = ConvertToEntityByBusinessData(attachment, false);
            return entity.Save();
        }

        public bool RemoveAttachmentInfo(Guid attachmentId)
        {
            var attachmentsCollection = new AttachmentsCollection();
            int result = attachmentsCollection.DeleteMulti(AttachmentsFields.Id == attachmentId);
            return result > 0;
        }

        public AttachmentData GetAttachmentById(Guid attachmentId)
        {
            var meta = new LinqMetaData();
            var linqResult = (from q in meta.Attachments
                              where q.Id == attachmentId
                              select q).FirstOrDefault();

            if (linqResult != null)
            {
                return ConvertToBusinessDataByEntity(linqResult);
            }

            return null;
        }

        public List<AttachmentData> GetAttachmentByBusinessId(string businessId)
        {
            var meta = new LinqMetaData();
            var listResult = from q in meta.Attachments
                              where q.BusinessId == businessId
                              select q;
            List<AttachmentData> listAttachments = new List<AttachmentData>();
            if(listResult!=null && listResult.Count()>0)
            {
                foreach (var attachmentsEntity in listResult)
                {
                    var obj = ConvertToBusinessDataByEntity(attachmentsEntity);
                    if(obj!=null)
                    {
                        listAttachments.Add(obj);
                    }
                }
            }

            return listAttachments;
        }

        private AttachmentsEntity ConvertToEntityByBusinessData(AttachmentData attachmentData,bool isAdd)
        {
            AttachmentsEntity entity = null;
            if(isAdd)
            {
                entity=new AttachmentsEntity();
                entity.Id = attachmentData.Id;
                if(entity.Id==Guid.Empty)
                {
                    entity.Id = Guid.NewGuid();
                }
            }
            else
            {
                entity=new AttachmentsEntity(attachmentData.Id);
            }
            entity.BusinessId = attachmentData.BusinessId;
            entity.AttachmentType = (int)attachmentData.AttachmentType;
            entity.DisplayName = attachmentData.DisplayName;
            entity.Postfix = attachmentData.Postfix;
            entity.FileName = attachmentData.FileName;
            entity.SavePath = attachmentData.SavePath;
            entity.Note = attachmentData.Note;
            entity.CreateDate = attachmentData.CreateDate;
            entity.ModifyDate = attachmentData.ModifyDate;

            return entity;
        }

        private  AttachmentData ConvertToBusinessDataByEntity(AttachmentsEntity attachmentEntity)
        {
            return new AttachmentData
                       {
                           Id = attachmentEntity.Id,
                           BusinessId = attachmentEntity.BusinessId,
                           AttachmentType = (AttachmentType)attachmentEntity.AttachmentType,
                           DisplayName = attachmentEntity.DisplayName,
                           Postfix = attachmentEntity.Postfix,
                           FileName = attachmentEntity.FileName,
                           SavePath = attachmentEntity.SavePath,
                           Note = attachmentEntity.Note,
                           CreateDate = Convert.ToDateTime(attachmentEntity.CreateDate),
                           ModifyDate = Convert.ToDateTime(attachmentEntity.ModifyDate)
                       };
        }

        
    }
}
